package mall.kotlin.com.myapplication.material_design;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

/**
 * USER: zjw
 * DATA: 2018/10/30 0030
 * TIME: 下午 4:42
 */
public class FooterBehavior extends CoordinatorLayout.Behavior<View> {
    private int directionChange;

    public FooterBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //第二种方法
    @Override
    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
       return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        float y = Math.abs(dependency.getY());
        child.setTranslationY(y);
        return true;
       /* if (deltaY == 0) {
            deltaY = dependency.getY() - child.getHeight();
        }

        float dy = dependency.getY() - child.getHeight();
        dy = dy < 0 ? 0 : dy;
        float alpha = 1 - (dy / deltaY);
        child.setAlpha(alpha);

        return true;*/

    }


    //第一种方法
    /**
     * 方法的返回值表明这次滑动我们要不要关心,我们关心的是y轴方向的,
     *
     * @param coordinatorLayout
     * @param child
     * @param directTargetChild
     * @param target
     * @param axes
     * @return
     */
    /*@Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes) {
        Log.i("111--->", "axes--->" + axes + "   --->2");
        return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    *//**
     * 用于处理滑动,
     * 如果滑动的距离累加值 directionChange 大于 Linearlayout的高度,并且LinearLayout是显示状态,则隐藏
     * 如果 directionChange <0 ,并且linearLayout是gone状态,则显示
     *
     * @param coordinatorLayout
     * @param child             Linearlayout
     * @param target
     * @param dx
     * @param dy                滑动距离,先上滑动为正值,向下为负值
     * @param consumed
     *//*
    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed) {
        Log.i("222--->", "dy--->" + dy + "   directionChange--->" + directionChange);
        if (dy > 0 && directionChange < 0 || dy < 0 && directionChange > 0) {
            child.animate().cancel();
            directionChange = 0;
        }
        directionChange += dy;
        if (directionChange > child.getHeight() && child.getVisibility() == View.VISIBLE) {
            hide(child);
        } else if (directionChange < 0 && child.getVisibility() == View.GONE) {
            show(child);
        }
    }

    private void show(final View child) {
        ViewPropertyAnimator animator = child.animate().translationY(0).setInterpolator(new FastOutSlowInInterpolator()).setDuration(20);
        animator.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                child.setVisibility(View.VISIBLE);
            }
        });
    }

    private void hide(final View child) {
        ViewPropertyAnimator animator = child.animate().translationY(child.getHeight()).setInterpolator(new FastOutSlowInInterpolator()).setDuration(20);
        animator.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                child.setVisibility(View.GONE);
            }
        });
    }*/

}
